package cn.rokhdelar.ams.management.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.common.domain.vo.PageVO;
import cn.rokhdelar.ams.management.domain.po.SysDictionary;
import cn.rokhdelar.ams.management.domain.query.DictionaryQuery;
import cn.rokhdelar.ams.management.domain.vo.DictionaryItemVO;
import cn.rokhdelar.ams.management.domain.vo.DictionaryVO;
import cn.rokhdelar.ams.management.service.SysDictionaryItemService;
import cn.rokhdelar.ams.management.service.SysDictionaryService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rokhdelar
 * @since 2024/10/21 -- 17:30
 **/
@RestController
@RequestMapping(value = "/management/dictionaries")
@RequiredArgsConstructor
public class DictionaryController {
    private final SysDictionaryService dictionaryService;
    private final SysDictionaryItemService dictionaryItemService;

    @GetMapping
    @Operation(summary = "根据条件分页查询权限")
    public ResponseEntity<PageVO<DictionaryVO>> getDictionaryListByPage(@ParameterObject DictionaryQuery query) {
        return ResponseEntity.ok(dictionaryService.getDictionaryListByPage(query));
    }

    @GetMapping(value = "/{id}")
    @Operation(summary = "通过ID查询字典信息")
    public ResponseEntity<DictionaryVO> getDictionaryById(@PathVariable String id) {
        return ResponseEntity.ok(dictionaryService.getDictionaryById(id));
    }

    @PostMapping
    @Operation(summary = "新增字典")
    public ResponseEntity<Boolean> createDictionary(@RequestBody DictionaryVO dictionary) {
        return ResponseEntity.ok(dictionaryService.save(BeanUtil.copyProperties(dictionary, SysDictionary.class)));
    }

    @PatchMapping
    @Operation(summary = "更新字典")
    public ResponseEntity<Boolean> updateDictionary(@RequestBody DictionaryVO dictionary) {
        return ResponseEntity.ok(dictionaryService.updateById(BeanUtil.copyProperties(dictionary, SysDictionary.class)));
    }

    @DeleteMapping(value = "/{id}")
    @Operation(summary = "根据ID删除字典，字典必须为空")
    public ResponseEntity<Boolean> deleteDictionary(@PathVariable String id) {
        return ResponseEntity.ok(dictionaryService.deleteById(id));
    }

    @GetMapping(value = "/{dictionaryId}/items")
    @Operation(summary = "查询字典的所有字典项")
    public ResponseEntity<List<DictionaryItemVO>> getDictionaryItem(@PathVariable String dictionaryId) {
        return ResponseEntity.ok(dictionaryItemService.getDictionaryItemList(dictionaryId));
    }

    @GetMapping(value = "/by-key/items/{key}")
    @Operation(summary = "通过字典键查询字典项")
    public ResponseEntity<List<DictionaryItemVO>> getDictionaryItemListByKey(@PathVariable String key) {
        return ResponseEntity.ok(dictionaryItemService.getDictionaryItemListByKey(key));
    }

    @GetMapping(value = "/items/{itemId}")
    @Operation(summary = "通过字典项ID查询字典项信息")
    public ResponseEntity<DictionaryItemVO> getDictionaryItemById(@PathVariable String itemId) {
        return ResponseEntity.ok(dictionaryItemService.getDictionaryItemById(itemId));
    }

    @PostMapping(value = "/{dictionaryId}/items")
    @Operation(summary = "为指定字典添加新的字典项")
    public ResponseEntity<Boolean> addDictionaryItem(@PathVariable String dictionaryId, @RequestBody DictionaryItemVO dictionaryItemVO) {
        return ResponseEntity.ok(dictionaryItemService.addDictionaryItem(dictionaryId, dictionaryItemVO));
    }

    @PatchMapping(value = "/items/{itemId}")
    @Operation(summary = "更新字典项")
    public ResponseEntity<Boolean> updateDictionaryItem(@PathVariable String itemId, @RequestBody DictionaryItemVO dictionaryItemVO) {
        return ResponseEntity.ok(dictionaryItemService.updateDictionaryItemById(itemId, dictionaryItemVO));
    }

    @DeleteMapping(value = "/items/{itemId}")
    @Operation(summary = "删除字典项")
    public ResponseEntity<Boolean> deleteDictionaryItem(@PathVariable String itemId) {
        return ResponseEntity.ok(dictionaryItemService.deleteDictionaryItemById(itemId));
    }
}
